//
//  JOMeController.m
//  新浪微博模拟
//
//  Created by song on 15/3/24.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JOMeController.h"
#import "JOMeInfo.h"
#import "JOMeCell.h"
#import "MJRefresh.h"
#import "UMSocial.h"
#import "SVProgressHUD/SVProgressHUD.h"
#import "MBProgressHUD/MBProgressHUD.h"


#define  ME   @"ME"
@interface JOMeController ()

@end

@implementation JOMeController



// IOS 通过NSThread 来创建线程，有两种方式 IOS 通过NSThread 来创建线程，有两种方式 IOS 通过NSThread 来创建线程，有两种方式 IOS 通过NSThread 来创建线程，有两种方式 IOS 通过NSThread 来创建线程，有两种方式 IOS 通过NSThread 来创建线程，有两种方式
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.infoarray = [NSMutableArray array];

    [self setupRefresh];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight =44.0;
    [self.tableView registerNib:[UINib nibWithNibName:@"JOMeCell" bundle:nil] forCellReuseIdentifier:@"ME"];
//    [self svprocess];
    [self MBProcessHUD];
    
    
    
    
//    
//    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55f92a1d67e58e822f000034" shareText:@"测试" shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina ,nil] delegate:nil];

}

-(void)svprocess
{
    [SVProgressHUD showWithStatus:@"加载中，请稍后。。。"];
    [self performSelector:@selector(dismiss) withObject:nil afterDelay:3];
}

-(void)MBProcessHUD
{
    MBProgressHUD *HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.delegate = self;
    HUD.color = [UIColor blackColor];
    HUD.labelText =@"hello word";
    HUD.detailsLabelText =@"test tail good";
    HUD.dimBackground =YES ; // 是否遮盖
    [HUD hide:YES afterDelay:3];
}

-(void)dismiss
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW ,(int64_t)(5.0 *NSEC_PER_SEC)) , dispatch_get_main_queue(), ^{
        
         [self loadNewData];
        [SVProgressHUD dismiss];
    });
    
}

-(void)setupRefresh
{
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadNewData];
    }];
    
//    [self.tableView.header beginRefreshing];

    
}

-(void)loadNewData
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       
        for (int i = 0; i< 10 ; i++) {
            JOMeInfo *info = [[JOMeInfo alloc] init];
            info.title=[NSString stringWithFormat:@"%d hello word",arc4random() %1000];
            info.content =@"IOS 通过NSThread 来创建线程，有两种方式 OS sjdfisaidfsnnsfdfsdnfoskndfosanfspidnfsiodnfsiodfnsiosnfskdfnskdfnskdfnsdkfn";
            info.imagepath=@"";
            [self.infoarray insertObject:info atIndex:0];
            
        }
        // 刷新表格
        [self.tableView reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.tableView.header endRefreshing];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.infoarray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
      JOMeCell *cell =[tableView dequeueReusableCellWithIdentifier:@"ME"];
      cell.info = self.infoarray[indexPath.row];
    
    
    cell.bounds = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds) , CGRectGetHeight(tableView.bounds));
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    
    
    CGFloat hight =[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    hight = hight+ 1.0f;
    return  200;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

    JOMeCell *cell =[tableView dequeueReusableCellWithIdentifier:@"ME"];
    if(cell==nil)
    {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ME"];
    }
    cell.info = self.infoarray[indexPath.row];
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    return cell;
}




@end
